home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_5_5.arc / KERNEL.ASM < prev    next >
Assembly Source File  |  1987-08-07  |  15KB  |  272 lines

  1. ;   Source code for TSR, by John J. Newlin, Page 28, Volume 5.5,
  2. ;                   Programmer's Journal
  3. ;
  4. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5. ;RAM resident KERNEL driver for main program in expanded memory
  6. ;Copyright 1987 by John J. Newlin, 4060-228 Rosenda Court, San
  7. ;Diego, CA 92122
  8. ;All rights reserved
  9. ;Assemble with MASM, link with LINK, convert to .COM with EXE2BIN
  10.  
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. ;Equates referencing offset addresses in the main program.  Addresses
  13. ;derived from Turbo Pascal main program - see article text.
  14. activate      equ   2D9FH
  15. demo_main     equ   2DA6H
  16. emm_handle    equ   2DAAH
  17. loaded_in_emm equ   2DA8H
  18.  
  19. code segment
  20.      assume cs:code
  21. org 100h
  22.  
  23. begin:
  24.        jmp initialize             ;jump directly to initialization code
  25.  
  26. demo LABEL DWORD                  ;label for far call to main prog
  27. demo_ofs            dw 0          ;offset of main prog handler
  28. demo_seg            dw 0          ;segment of main prog
  29. i16 LABEL DWORD                   ;label for far call/jump to int 16h
  30. i16_ofs             dw 0          ;offset of original int 16h
  31. i16_seg             dw 0          ;segment of original int 16h
  32. save_ss             dw 0          ;save caller's SS reg
  33. save_sp             dw 0          ;save caller's SP reg
  34. save_ax             dw 0          ;save caller's AX reg
  35. hotkey              db 0          ;hotkey value
  36. active              db 0          ;active flag
  37. handle              dw 0          ;EMM handle
  38. local_stack         dw 20 dup(0)  ;local stack area
  39. stkptr              dw 0          ;local SP
  40.  
  41. save_map:          push ax
  42.                    push dx
  43.                    mov dx,cs:handle                 ;DX gets our handle
  44.                    mov ah,47h                       ;save mapping context
  45.                    int 67h                          ;call EMM
  46.                    pop dx
  47.                    pop ax
  48.                    ret                              ;
  49.  
  50. restore_map:
  51.                    push ax
  52.                    push dx
  53.                    mov dx,cs:handle                 ;DX gets handle
  54.                    mov ah,48h                       ;to restore mapping context
  55.                    int 67h                          ;call EMM
  56.                    pop dx
  57.                    pop ax
  58.                    ret
  59.  
  60. map_memory:
  61.                    push ax
  62.                    push bx
  63.                    push cx
  64.                    push dx
  65.                    mov cx,4                         ;will map 4 pages
  66. do_again:          mov dx,cs:handle                 ;DX gets handle
  67.                    mov bx,cx                        ;BX gets logical page
  68.                    mov al,cl                        ;AL gets physical page
  69.                    dec bx                           ;decrement logical page
  70.                    dec al                           ;decrement physical page
  71.                    mov ah,44h                       ;set up for EMM call
  72.                    int 67h                          ;call EMM
  73.                    loop do_again                    ;do again (3,2,1,0)
  74.                    pop ax
  75.                    pop bx
  76.                    pop cx
  77.                    pop dx
  78.                    ret                              ;
  79.  
  80. run_demo:
  81.                    call save_map                    ;save mapping context
  82.                    call map_memory                  ;map our pages in
  83.                    mov cs:active,1                  ;set active flag
  84.                    mov cs:save_ax,ax                ;save AX reg
  85.                    cli                              ;turn off interrupts
  86.                    mov cs:save_ss,ss                ;save caller's SS reg
  87.                    mov cs:save_sp,sp                ;save caller's SP reg
  88.                    mov ax,cs                        ;get our CS
  89.                    mov ss,ax                        ;put in SS
  90.                    mov ax,offset cs:stkptr          ;get offset of local stack
  91.                    mov sp,ax                        ;put in SP
  92.                    sub sp,2                         ;decrement SP
  93.                    sti                              ;enable interrupts
  94.                    push ax                          ;save
  95.                    push bx                          ; caller's
  96.                    push cx                          ;  regs
  97.                    push dx                          ;   on
  98.                    push di                          ;    our
  99.                    push si                          ;     local
  100.                    push bp                          ;      stack
  101.                    push es                          ;
  102.                    call cs:[demo]                   ;call our main prog
  103.                    pop es                           ;restore
  104.                    pop bp                           ; caller's
  105.                    pop si                           ;  regs
  106.                    pop di                           ;   from
  107.                    pop dx                           ;    our
  108.                    pop cx                           ;     local
  109.                    pop bx                           ;      stack
  110.                    pop ax                           ;
  111.                    cli                              ;turn off interrupts
  112.                    mov ax,cs:save_ss                ;get caller's SS reg
  113.                    mov ss,ax                        ;put in SS
  114.                    mov sp,cs:save_sp                ;restore caller's SP
  115.                    mov ax,cs:save_ax                ;restore caller's AX
  116.                    sti                              ;enable interrupts
  117.                    mov cs:active,0                  ;reset active flag
  118.                    call restore_map                 ;restore mapping context
  119.                    ret                              ;return
  120.  
  121. int16              proc far                         ;use far attr for returns
  122.                    cmp ah,98h                       ;regular int 16 request?
  123.                    jb keys16                        ;yes
  124.  
  125. n00:               cmp ah,9Fh                       ;request to remove?
  126.                    jne l1                           ;no
  127.                    mov ax,cs:demo_seg               ;yes, prepare to remove
  128.                    mov es,ax                        ;ES gets frame segment
  129.                    mov ah,45h                       ;prepare to release EM
  130.                    mov dx,cs:handle                 ;load handle in DX
  131.                    int 67h                          ;call EMM to release
  132.                    cli                              ;turn of interrupts
  133.                    push ds                          ;save DS
  134.                    mov ax,cs:i16_seg                ;get orginal int 16h seg
  135.                    mov ds,ax                        ;put in DS
  136.                    mov dx,cs:i16_ofs                ;DX = original int 16h ofs
  137.                    mov ax,2516h                     ;set up to re-install
  138.                    int 21h                          ;call DOS to do it
  139.                    pop ds                           ;restore ds
  140.                    mov bx,2Ch                       ;2Ch in PSP has segment
  141.                    mov ax,cs:[bx]                   ;get mem block segment
  142.                    mov es,ax                        ;put in ES for DOS
  143.                    mov ah,49h                       ;set up to release
  144.                    int 21h                          ;call DOS to release
  145.                    mov ax,cs                        ;our segment
  146.                    mov es,ax                        ;put in ES for DOS
  147.                    mov ah,49h                       ;set up to release
  148.                    int 21h                          ;call DOS to release
  149.                    sti                              ;enable interrupts
  150.                    mov ah,4Ch                       ;set up to teminate
  151.                    int 21h                          ;call DOS to terminate
  152.  
  153. l1:
  154.                    cmp ah,9Bh                       ;requ for presence flag?
  155.                    jne keys16                       ;no
  156.                    mov ax,0ABCDh                    ;indicator in AX
  157.                    iret                             ;return to caller
  158.  
  159. no_action16:
  160.                    jmp cs:[i16]                     ;jmp to original int 16h
  161.  
  162. keys16:
  163.                    cmp cs:active,1                  ;is demo active?
  164.                    je no_action16                   ;yes - depart
  165.                    cmp ah,1                         ;buffer status req?
  166.                    ja no_action16                   ;no - depart
  167.                    je checkchar                     ;yes - process
  168.  
  169. getchar:           mov ah,0                         ;set up to get a char
  170.                    pushf                            ;to fake int call
  171.                    call cs:[i16]                    ;call original int 16h
  172.                    cmp al,0                         ;alt key pressed?
  173.                    jne exit0                        ;no - depart
  174.                    cmp ah,cs:hotkey                 ;our hot key?
  175.                    je action1                       ;yes - process
  176. exit0:             iret                             ;no - return to caller
  177.  
  178. action1:
  179.                    call run_demo                    ;call our main prog
  180.                    jmp getchar                      ;go check key buffer
  181.  
  182. checkchar:
  183.                    mov ah,1                         ;set up to check buffer
  184.                    pushf                            ;to fake int call
  185.                    call cs:[i16]                    ;call original int 16h
  186.                    pushf                            ;save flags
  187.                    jz exit16                        ;no key in buffer - depart
  188.                    cmp al,0                         ;alt key pressed?
  189.                    jne exit16                       ;no - depart
  190.                    cmp ah,cs:hotkey                 ;our hot key pressed?
  191.                    jne exit16                       ;no - depart
  192.                    mov ah,0                         ;remove key from buffer
  193.                    call cs:[i16]                    ;call original int 16h
  194.                    call run_demo                    ;call our main prog
  195.                    jmp checkchar                    ;go check buffer
  196.  
  197. exit16:
  198.                    popf                             ;discard flags
  199.                    ret 2                            ;return to caller
  200. int16              endp
  201.  
  202.  
  203. initialize         proc
  204.                    mov ax,cs                     ;get code seg value
  205.                    mov ds,ax                     ;set DS to same
  206. continue:          mov ah,9Bh                    ;set up for int 16 query
  207.                    int 16h                       ;call it
  208.                    cmp ax,0ABCDh                 ;is DEMO loaded?
  209.                    jne not_loaded                ;no, continue
  210.                    mov dx,offset loaded_msg      ;yes, it's loaded
  211.                    mov ah,9                      ;set up to write msg
  212.                    int 21h                       ;write it
  213.                    mov ah,4Ch                    ;terminate
  214.                    int 21h
  215. not_loaded:        mov ax,3567h                  ;set up to get int 67h seg
  216.                    int 21h                       ;call DOS
  217.                    mov di,10                     ;offset of EMM string
  218.                    mov si,offset emm_name        ;offset of our string
  219.                    mov cx,8                      ;8 chars to check
  220.                    cld                           ;set direction flag forward
  221.                    repz cmpsb                    ;compare string
  222.                    jz emm_ok                     ;compare ok?
  223.                    mov dx,offset emm_msg         ;no
  224.                    mov ah,9                      ;set up for error msg
  225.                    int 21h                       ;call DOS to write
  226.                    mov ah,4Ch                    ;set up to terminate
  227.                    int 21h                       ;terminate
  228. emm_ok:            mov ah,41h                    ;set up to get EMM segment
  229.                    int 67h                       ;call EMM
  230.                    mov cs:demo_seg,bx            ;save it
  231.                    mov es,bx                     ;put it in ES
  232.                    mov ax,es:[loaded_in_emm]     ;get DEMO flag
  233.                    cmp ax,9876h                  ;is it there?
  234.                    je demo_present               ;yes
  235.                    mov dx,offset not_loaded_msg  ;no
  236.                    mov ah,9                      ;set up for error msg
  237.                    int 21h                       ;call DOS to write
  238.                    mov ah,4Ch                    ;set up to terminate
  239.                    int 21h                       ;terminate
  240. demo_present:      mov al,byte ptr es:[activate] ;get hot key
  241.                    mov hotkey,al                 ;save it
  242.                    mov ax,word ptr es:[emm_handle] ;get EMM handle
  243.                    mov handle,ax                 ;save it
  244.                    mov ax,word ptr es:[demo_main];get DEMO handler offset
  245.                    mov demo_ofs,ax               ;save it
  246.  
  247.                    mov ax,3516h                  ;set up to get int16 addr
  248.                    int 21h                       ;call DOS to get it
  249.                    mov i16_seg,es                ;save segment
  250.                    mov i16_ofs,bx                ;save offset
  251.                    mov ax,2516h                  ;set up to replace with ours
  252.                    mov dx,offset int16           ;offset into DX
  253.                    int 21h                       ;call DOS to install
  254.  
  255.                    mov dx,offset sign_on         ;sign on msg offset
  256.                    mov ah,9                      ;set up to write
  257.                    int 21h                       ;call DOS to write
  258.  
  259.                    mov dx,offset initialize      ;last byte of resident code
  260.                    int 27h                       ;terminate & stay resident
  261.  
  262. emm_name            db 'EMMXXXX0',0
  263. emm_msg             db 'No EMM available - aborting',7,'$'
  264. loaded_msg          db 'KERNEL in memory - aborting',7,'$'
  265. sign_on             db 'KERNEL is loaded and DEMO is operational in EMS','$'
  266. not_loaded_msg      db 'DEMO not in expanded memory',7,'$'
  267.  
  268. initialize         endp
  269. code               ends
  270.                    end begin
  271.  
  272.